﻿using CodeCarvings.Piczard;
using LuShanTea.Admin.Web.App_Start;
using LuShanTea.Admin.Web.Models;
using LuShanTea.Common;
using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Admin.Web.Controllers
{
    public class AdminUserController : Controller
    {
        public IAdminUserService AdminUserService { get; set; }

        public IRoleService RoleService { get; set; }

        public ICityService CityService { get; set; }

        public IAreaService AreaService { get; set; }

        // GET: AdminUser
        [CheckPermission("AdminUser.List")]
        public ActionResult List()
        {
            var model = AdminUserService.GetAll();

            return View(model);
        }

        [HttpPost]
        [CheckPermission("AdminUser.Delete")]
        public ActionResult Delete(long id)
        {
            AdminUserService.MarkDelete(id);
            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpGet]
        [CheckPermission("AdminUser.Edit")]
        public ActionResult Edit(long id)
        {
            AdminUserEditListModel model = new AdminUserEditListModel();
            model.AdminUser = AdminUserService.GetById(id);
            if (model.AdminUser == null)
            {
                return Content("找不到该管理员");
            }

            model.HROles = RoleService.GetByAdminUserId(id);
            model.Roles = RoleService.GetAll();

            return View(model);
        }

        [HttpPost]
        [CheckPermission("AdminUser.Edit")]
        public ActionResult Edit(AdminUserEditSubModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }
            var en = AdminUserService.GetByEmailOrPhone(model.Email, model.PhoneNum);
            if (en != null)
            {
                if (en.Id != model.Id)
                {
                    return Json(new AjaxResult { Status = "error", ErrorMsg = "邮箱或手机已存在" });
                }
            }
            AdminUserAddUpdateDto dto = new AdminUserAddUpdateDto();
            dto.Id = model.Id;
            dto.Email = model.Email;
            dto.Gender = model.Gender;
            dto.ImgSrc = model.ImgSrc;
            dto.Name = model.Name;
            dto.NickName = model.NickName;
            dto.PhoneNum = model.PhoneNum;
            AdminUserService.Update(dto);
            RoleService.UpdateRoles(model.Id, model.Roles);
            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpGet]
        [CheckPermission("AdminUser.Add")]
        public ActionResult AddNew()
        {
            var model = RoleService.GetAll();
            return View(model);
        }

        [HttpPost]
        [CheckPermission("AdminUser.Add")]
        public ActionResult AddNew(AdminUserAddSubModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }
            var en = AdminUserService.GetByEmailOrPhone(model.Email, model.PhoneNum);
            if (en != null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "邮箱或手机已存在" });
            }
            AdminUserAddUpdateDto dto = new AdminUserAddUpdateDto();
            dto.Email = model.Email;
            dto.Gender = model.Gender;
            dto.ImgSrc = ("http://static.amortal.top/" + model.ImgSrc);
            dto.Name = model.Name;
            dto.NickName = model.NickName;
            dto.PassWord = model.PassWord;
            dto.PhoneNum = model.PhoneNum;
            long adminUserId = AdminUserService.AddNew(dto);
            RoleService.AddRole(adminUserId, model.Roles);
            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpPost]
        [CheckPermission("AdminUser.UpLoadImg")]
        public ActionResult Upload(HttpPostedFileBase file)
        {
            if (file.ContentLength <= 0 || file.ContentLength >= 1024 * 1024 * 2)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "文件过大或过小" });
            }

            string md5 = CommonHelper.CalcMD5(file.InputStream);
            string ext = Path.GetExtension(file.FileName);

            string[] exts = { ".jpg", ".png", "gif", ".jpeg", ".bmp" };

            if (!exts.Contains(ext))
            {
                return Json(new AjaxResult
                {
                    Status = "error",
                    ErrorMsg = "请勿上传非图片文件,支持格式( .jpg, .png, gif, .jpeg, .bmp)"
                });
            }

            string path = "upload/userImg/" + DateTime.Now.ToString("yyyy/MM/dd") + "/" + md5 + ext;
            //指针归零
            file.InputStream.Position = 0;
            UploadFile uploadFile = new UploadFile();

            AjaxResult ajaxResult = uploadFile.UploadImg(path, file.InputStream);


            return Json(new AjaxResult { Status = "ok", Data = path });
        }


    }
}